home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / classdoc.lha / classdoc / list.H < prev   
C/C++ Source or Header  |  1993-08-08  |  3KB  |  125 lines

  1. // Generic list package in C++.  Any kind of object can be the member of
  2. // such a list; only pointers to elements are stored.  An element can be
  3. // the member of many lists at a time.  Before an object is destructed,
  4. // it must explicitly be taken out of any lists (dangling pointer problem).
  5. //
  6. // This is a protected class, i.e., it is not possible to create instances
  7. // of it.  Derived classes should provide lists for specific types of objects.
  8. //
  9. // Implementation note.  The list is implemented as an array of pointers.
  10. // Inserting elements at the front of the list (Insert() and operator +=)
  11. // is cheap; removing elements (except the first) and inserting at the end
  12. // is more expensive.  Assignment and passing a list as a VALUE parameter
  13. // requires copying of the pointer array, and is therefore expensive; pass
  14. // lists as "const reference" parameters instead.
  15. //
  16. // Author: Dag Bruck. Date: 1989-07-14.
  17.  
  18.  
  19. #ifndef LIST_H
  20. #define LIST_H
  21.  
  22.  
  23. #include "defs.H"
  24. #include "generic.h"
  25.  
  26.  
  27. typedef void* Pointer;
  28.  
  29.  
  30. class GenericList {
  31. friend class GenericIterator;
  32.   
  33. public:
  34.   GenericList();
  35.   // Constructs a list with no elements.
  36.  
  37.   ~GenericList();
  38.   // Destructs list.  The elements in the list are not
  39.   // destructed.
  40.  
  41.   void Insert(Pointer);
  42.   void operator += (Pointer);
  43.   // Inserts an element first in list.
  44.  
  45.   void Append(Pointer);
  46.   // Inserts an element last in list.  Not as efficient as Insert().
  47.  
  48.   void Remove(Pointer);
  49.   // Removes an element from list. The element is not destructed.
  50.  
  51.   boolean Member(Pointer);
  52.   // Returns true if the element is a member of list.
  53.  
  54.   void Reverse();
  55.   // Reverses the order of the elements in the list.  The list is reversed
  56.   // in place, i.e., the original order is lost.
  57.   
  58.   Pointer First();
  59.   // Returns the first element of list, or nil if list is empty.
  60.  
  61.   Pointer Last();
  62.   // Returns the last element of list, or nil if list is empty.
  63.  
  64.   unsigned Length();
  65.   // Returns the number of elements in list.
  66.  
  67.   boolean Empty();
  68.   // Returns true if list is empty.
  69.  
  70.   GenericList(const GenericList&);
  71.   // Used for passing parameters and return values.
  72.  
  73.   void operator = (const GenericList&);
  74.   // Assignment.
  75.  
  76. private:
  77.   Pointer* p;
  78.   // Array of pointers to actual member objects.
  79.  
  80.   unsigned n;
  81.   // Number of elements in list.
  82.  
  83.   unsigned size;
  84.   // Maximum number of elements in list.
  85.  
  86.   void Expand();
  87.   // Expand array of pointers to cope with more elements.
  88. };
  89.  
  90.  
  91. inline void GenericList :: operator += (Pointer e)
  92. { Insert(e); }
  93.  
  94. inline unsigned GenericList :: Length()
  95. { return n; }
  96.  
  97. inline boolean GenericList :: Empty()
  98. { return n == 0; }
  99.  
  100.  
  101. // Ugly stuff to fake parameterized types.
  102.  
  103.  
  104. #define LIST(type)name2(type,List)
  105. #define LIST_TYPE(type) class LIST(type) : public GenericList { \
  106. public: \
  107. LIST(type)() {} \
  108. ~LIST(type)() {} \
  109. void Insert(type& x) { GenericList::Insert(&x); } \
  110. void Insert(type* x) { GenericList::Insert(x); } \
  111. void operator += (type& x) { GenericList::operator += (&x); } \
  112. void operator += (type* x) { GenericList::operator += (x); } \
  113. void Append(type& x) { GenericList::Append(&x); } \
  114. void Append(type* x) { GenericList::Append(x); } \
  115. void Remove(type& x) { GenericList::Remove(&x); } \
  116. void Remove(type* x) { GenericList::Remove(x); } \
  117. boolean Member(type& x) { return GenericList::Member(&x); } \
  118. boolean Member(type* x) { return GenericList::Member(x); } \
  119. type* First() { return (type *) GenericList::First(); } \
  120. type* Last() { return (type *) GenericList::Last(); } \
  121. }
  122.  
  123.  
  124. #endif
  125.